#region Using Directives

using System;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Services;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using EnvDTE;
using EnvDTE80;

#endregion

namespace WiGO.RCX.PF.Actions
{
    class ImportContentsAction : Action
    {
        #region Input Properties

        [Input]
        public string SourcePath
        {
            get { return sourcePath; }
            set { sourcePath = value; }
        } string sourcePath;

        [Input]
        public string DestPath
        {
            get { return destPath; }
            set { destPath = value; }
        } string destPath;

        #endregion

        #region Output Properties
        [Output]
        public string Result
        {
            get { return actionResult; }
            set { actionResult = value; }
        } string actionResult;

        #endregion

        #region IAction Members

        private List<string> copiedFiles = new List<string>();

        public override void Execute()
        {
            GuidancePackage gp = (GuidancePackage)GetService(typeof(IExecutionService));
            string gpBasePath = gp.BasePath;
            string srcAbstPath = String.Format(CultureInfo.InvariantCulture, @"{0}\\{1}", gpBasePath, sourcePath);
            DTE vs = GetService<DTE>(true);
            Solution2 currentSolution = vs.Solution as Solution2;
            string solutionPath = (string)currentSolution.Properties.Item("Path").Value;
            string dstAbstPath = String.Format(CultureInfo.InvariantCulture, @"{0}\\{1}", solutionPath, destPath);
            if (Directory.Exists(srcAbstPath) && Directory.Exists(destPath))
            {
                CopyContentsFromGP2Prj(srcAbstPath, destPath);
            }
        }

        private void CopyContentsFromGP2Prj(string srcPath, string destPath)
        {
            string[] files = Directory.GetFiles(srcPath);
            for (int i = 0; i < files.Length; i++)
            {
                string fileName = String.Format(CultureInfo.InvariantCulture, @"{0}\\{1}",srcPath,files[i]);
                FileInfo fi = new FileInfo(fileName);
                string destFileName = String.Format(CultureInfo.InvariantCulture, @"{0}\\{1}", destPath, files[i]);
                fi.CopyTo(destFileName);
            }
            string[] dirs = Directory.GetDirectories(srcPath);
            for (int i = 0; i < dirs.Length; i++)
            {
                string dstAbstPath = String.Format(CultureInfo.InvariantCulture, @"{0}\\{1}", destPath, dirs[i]);
                string srcAbstPath = String.Format(CultureInfo.InvariantCulture, @"{0}\\{1}", srcPath, dirs[i]);
                CopyContentsFromGP2Prj(srcAbstPath, dstAbstPath);
            }
        }

        public override void Undo()
        {
            // remove files
        }

        #endregion
    }
}
